﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace Himp.DataHub
{
    public class PatnMasterIdxManager : DomainService
    {
        public PatnMasterIdx CreatePatnMasterIdx(EnumMasterIdxType patnSource, string materIdxValue, string patnName
            , EnumGend gend, string? brdy)
        {
            var patnId = Guid.NewGuid().ToString();
            var patnBasInfo = new PatnMasterIdx(patnSource, materIdxValue, patnId, patnName, gend, brdy);

            //if (patnSrc == EnumPatnSource.门诊)
            //{
            //    patnBasInfo.PatnMasterIdxAssocOtpNos = new List<PatnMasterIdxAssocOtpNo>
            //    {
            //        new PatnMasterIdxAssocOtpNo() { OtpNo = Guid.NewGuid().ToString("N") }
            //    };
            //}
            //else if (patnSrc == EnumPatnSource.住院)
            //{
            //    patnBasInfo.PatnMasterIdxAssocIptNos = new List<PatnMasterIdxAssocIptNo>
            //    {
            //        new PatnMasterIdxAssocIptNo() { IptNo = Guid.NewGuid().ToString("N") }
            //    };
            //}

            return patnBasInfo;
        }

        public PatnMasterIdx CreatePatnMasterIdx(string idcard, string patnName)
        {
            var patnId = Guid.NewGuid().ToString();
            string brdy = GetBrdy(idcard);
            var gend = GetGend(idcard);
            var patnBasInfo = new PatnMasterIdx( EnumMasterIdxType.身份证号, idcard, patnId, patnName, gend, brdy);
            patnBasInfo.PatnMasterIdxAssocOtpNos = new List<string>
            {
                Guid.NewGuid().ToString("N")
            };

            //if (patnSrc == EnumPatnSource.门诊)
            //{
            //    patnBasInfo.PatnMasterIdxAssocOtpNos = new List<PatnMasterIdxAssocOtpNo>
            //    {
            //        new PatnMasterIdxAssocOtpNo() { OtpNo = Guid.NewGuid().ToString("N") }
            //    };
            //}
            //else if (patnSrc == EnumPatnSource.住院)
            //{
            //    patnBasInfo.PatnMasterIdxAssocIptNos = new List<PatnMasterIdxAssocIptNo>
            //    {
            //        new PatnMasterIdxAssocIptNo() { IptNo = Guid.NewGuid().ToString("N") }
            //    };
            //}

            return patnBasInfo;
        }

        private string GetBrdy(string idcard)
        {
            string pattern = @"^\d{6}(\d{4})(\d{2})(\d{2})\d{3}[0-9Xx]$";

            Match match = Regex.Match(idcard, pattern);

            if (match.Success)
            {
                int year = int.Parse(match.Groups[1].Value);
                int month = int.Parse(match.Groups[2].Value);
                int day = int.Parse(match.Groups[3].Value);

                return new DateTime(year, month, day).ToString("yyyy-MM-dd");
            }

            return null;
        }

        private EnumGend GetGend(string idcard)
        {
            string pattern = @"^\d{6}\d{8}(\d{1})(\d{1})\d{4}$";

            Match match = Regex.Match(idcard, pattern);

            if (match.Success)
            {
                int genderCode = int.Parse(match.Groups[2].Value);

                return genderCode % 2 == 0 ? EnumGend.女 : EnumGend.男;
            }

            return EnumGend.未知;
        }

        private string GetNaty(string idcard)
        {
            string pattern = @"^\d{6}\d{8}\d{2}(\d{2})\d{2}\d{2}\d{4}$";

            Match match = Regex.Match(idcard, pattern);

            if (match.Success)
            {
                string nationCode = match.Groups[1].Value;
                return nationCode;
            }

            return string.Empty;
        }
    }
}
